﻿using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Mvc;

namespace StudyCompanion.Commons.Result;

public class CommonResult
{
    public CommonResult()
    {
    }

    public CommonResult(int code, string msg)
    {
        Code = code;
        Message = msg;
    }

    /// <summary>
    ///     是否成功
    /// </summary>
    public bool IsSuccese => Code == 200;

    public int Code { get; set; } = 200;

    public string Message { get; set; } = "成功!";

    public static implicit operator CommonResult([NotNull] ProblemDetails problemDetails)
    {
        return new CommonResult
        {
            Code = problemDetails.Status ?? 400,
            Message = problemDetails.Detail ?? string.Empty
        };
    }
}

/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommonResult<T>
{
    /// <summary>
    /// </summary>
    public CommonResult()
    {
    }

    public CommonResult(T value)
    {
        Data = value;
    }

    /// <summary>
    /// </summary>
    /// <param name="code"></param>
    /// <param name="msg"></param>
    public CommonResult(int code, string msg)
    {
        Code = code;
        Message = msg;
    }

    /// <summary>
    /// </summary>
    /// <param name="code"></param>
    /// <param name="msg"></param>
    /// <param name="value"></param>
    public CommonResult(int code, string msg, T value)
    {
        Code = code;
        Message = msg;
        Data = value;
    }

    /// <summary>
    ///     回传的结果
    /// </summary>
    public T Data { get; set; }

    public int Code { get; set; } = 200;

    public string Message { get; set; } = "成功!";

    /// <summary>
    ///     是否成功
    /// </summary>
    public bool IsSuccese => Code == 200;

    /// <summary>
    /// </summary>
    /// <param name="result"></param>
    public static implicit operator CommonResult<T>(CommonResult result)
    {
        return new CommonResult<T>(result.Code, result.Message);
    }

    public static implicit operator CommonResult<T>([NotNull] ProblemDetails problemDetails)
    {
        return new CommonResult<T>
        {
            Code = problemDetails.Status ?? 400,
            Message = problemDetails.Detail ?? string.Empty
        };
    }

    /// <summary>
    /// </summary>
    /// <param name="value"></param>
    public static implicit operator CommonResult<T>(T value)
    {
        return new CommonResult<T>(value);
    }
}